package com.heifeng.cloudshelf.mvp.search.activity;

import android.app.Activity;
import android.content.Intent;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseActivity;
import com.heifeng.cloudshelf.event.ImageClickEvent;
import com.heifeng.cloudshelf.mvp.programme.activity.VideoPlayerActivity;
import com.heifeng.cloudshelf.mvp.search.contract.DetailActivityContract;
import com.heifeng.cloudshelf.mvp.search.presenter.DetailActivityPresenter;
import com.heifeng.cloudshelf.mvp.web.activity.WebViewActivity;
import com.heifeng.cloudshelf.utils.RichTextUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/*
 *搜搜我家详情  晒晒我家详情 共用界面
 */
public class DetailActivity extends BaseActivity<DetailActivityPresenter> implements DetailActivityContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

//    @BindView(R.id.iv_main)
//    ImageView ivMain;

    @BindView(R.id.iv_code)
    ImageView iv_code;

    @BindView(R.id.tv_all_num)
    TextView tvAll;

    @BindView(R.id.tv_select)
    TextView tvSelect;

    @BindView(R.id.tv1)
    TextView textView1;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.l_video)
    LinearLayout l_video;

    @BindView(R.id.l_panorama)
    LinearLayout l_panorama;


    private int selectPosition = 0;

    private int type = 0;
    private RichTextUtils instance;
    private String url;
    private String vidoeUrl;

//    public static void open(Context context) {
//        IntentUtil.startActivity(context, DetailActivity.class);
//    }

    public static void StartActivity(Activity activity, int type, String id) {
        Intent intent = new Intent(activity, DetailActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initEventAndData() {
        EventBus.getDefault().register(this);
        type = getIntent().getIntExtra("type", 0);
        instance = RichTextUtils.getInstance(this, this);

        mPresenter.init();
        String id = getIntent().getStringExtra("id");
        mPresenter.getHouseTypeInfo(id);
//        mPresenter.init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        instance.destory();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    public TextView getTvAllNum() {
        return tvAll;
    }

    @Override
    public TextView getTvSelect() {
        return tvSelect;
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public void setCodeImage(String url) {
        if (!TextUtils.isEmpty(url)) {
            Glide.with(this).load(url).apply(new RequestOptions().error(R.mipmap.ic_wxacode)).into(iv_code);
        }
    }

    @Override
    public void setImageTitle(String title) {
        instance.setRichText(title, textView1);
    }

    @Override
    public ViewPager getViewPager() {
        return viewPager;
    }

    @Override
    public void setViewImage(String url, String video) {
        this.url = url;
        this.vidoeUrl = video;
        if (TextUtils.isEmpty(url)) {
            l_panorama.setVisibility(View.GONE);
        } else {
            l_panorama.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(video)) {
            l_video.setVisibility(View.GONE);
        } else {
            l_video.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.iv_left, R.id.iv_right, R.id.l_back, R.id.l_video, R.id.l_panorama})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                recyclerView.scrollToPosition(getLeftScrollNum());
                break;
            case R.id.iv_right:
                recyclerView.scrollToPosition(getRightScrollNum());
                break;

            case R.id.l_panorama://全景
                if (!TextUtils.isEmpty(url)) {
                    WebViewActivity.open(this, url);
                }
                break;
            case R.id.l_video://视频
                if (!TextUtils.isEmpty(vidoeUrl)) {
                    VideoPlayerActivity.StartActivity(this, vidoeUrl);
                }
                break;

            case R.id.l_back:
                finish();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void LoadMoreEvent(ImageClickEvent event) {
//        Glide.with(mContext)
//                .load(event.getImageUrl())
//                .into(ivMain);

        tvSelect.setText("" + (event.getSelectPos() + 1));

        selectPosition = event.getSelectPos();

        viewPager.setCurrentItem(selectPosition);
    }

    private static int scrollnum = 7;//一次滑动的个数

    public int getLeftScrollNum() {
        if (mPresenter.getFirstItemPos() > 0) {
            if (mPresenter.getFirstItemPos() - scrollnum > 0)
                return mPresenter.getFirstItemPos() - scrollnum;
            else
                return 0;
        } else
            return 0;
    }

    public int getRightScrollNum() {
        if (mPresenter.getLastItemPos() < mPresenter.getList().size() - 1) {
            if (mPresenter.getLastItemPos() + scrollnum < mPresenter.getList().size() - 1) {
                return mPresenter.getLastItemPos() + scrollnum;
            } else {
                return mPresenter.getList().size() - 1;
            }
        } else
            return mPresenter.getList().size() - 1;
    }
}
